package com.iwith.dongmoon.listadpater;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.iwith.dongmoon.activity.R;
import com.iwith.dongmoon.gsonlist.BoardList;
import com.iwith.dongmoon.gsonlist.BoardListItem;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.ImageDownloader;
import com.iwith.dongmoon.util.RequestData;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.SessionManager;
import com.iwith.dongmoon.util.ImageDownloader.Mode;

public class CommentListAdapter extends ArrayAdapter<BoardListItem>{
	private Context mContext;
	private ArrayList<BoardListItem> items;
	private SessionManager mSessionManager;
	private LayoutInflater mInflater;
	private ImageDownloader mImageDownloader;

	public CommentListAdapter(Context context, int textViewResourceId, ArrayList<BoardListItem> list) {
		super(context, textViewResourceId, list);
		// TODO Auto-generated constructor stub

		this.mContext = context;
		this.items = list;;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mSessionManager=new SessionManager(mContext);
		this.mImageDownloader = new ImageDownloader();
		this.mImageDownloader.setMode(Mode.CORRECT);
		if(items!=null){
			for(int i=0; i<items.size(); i++){
				if(items.get(i).getFlag()==null){
					items.get(i).setFlag(false);
				}
			}
		}
	}
	
	class ViewHolder {
		LinearLayout layout;
		TextView content;
		TextView name;
		TextView datetime;
		ImageView btn_more;
		LinearLayout imagelayout;
		ImageView image;
		Button deletebt;
		
	}

	@Override
	public BoardListItem getItem(int position) {
		if(items==null) 	return null;
		else 				return items.get(position);
	}
	
	@Override
	public int getCount(){
		if(items==null)		return 0;
		else				return super.getCount();
	}

	public View getView(int position, View convertView, final ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder=new ViewHolder();
			convertView = mInflater.inflate(R.layout.listitem_comment, null);
			holder.layout = (LinearLayout) convertView.findViewById(R.id.listitemcomment_layout);
			holder.content = (TextView) holder.layout.findViewById(R.id.listitemcomment_content);
			holder.name = (TextView) holder.layout.findViewById(R.id.listitemcomment_username);
			holder.datetime = (TextView) holder.layout.findViewById(R.id.listitemcomment_datetime);
			holder.deletebt = (Button) holder.layout.findViewById(R.id.listitemcomment_delete);
			holder.btn_more = (ImageView) holder.layout.findViewById(R.id.listitemcomment_btn_more);
			holder.imagelayout = (LinearLayout) convertView.findViewById(R.id.listitemcomment_imagelayer);
			holder.image = (ImageView) holder.imagelayout.findViewById(R.id.listitemcomment_image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		final BoardListItem item = items.get(position);

		if (item != null) {
			holder.content.setText(item.getWr_content());
			holder.name.setText(item.getWr_name());
			holder.datetime.setText(item.getWr_datetime());
			holder.layout.setBackgroundColor((position & 1) == 1 ? 0xffececec : Color.WHITE);
			
			holder.imagelayout.setVisibility(View.GONE);
			final LinearLayout imageLayout = holder.imagelayout;
			final ImageView commentImage = holder.image;
			if(item.getImagePath().equals("") == false)
			{
				holder.btn_more.setVisibility(View.VISIBLE);
				holder.btn_more.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if(v.getTag() == null || (Boolean)v.getTag() == false)
						{
							v.setTag(true);
							imageLayout.setVisibility(View.VISIBLE);
							mImageDownloader.download(item.getImagePath(), commentImage);
							
						}
						else if((Boolean)v.getTag() == true)
						{
							v.setTag(false);
							imageLayout.setVisibility(View.GONE);
						}
					}
				});
			} else {
				holder.btn_more.setVisibility(View.INVISIBLE);
			}
			
			
			if(item.getFlag()==true){
				holder.deletebt.setVisibility(View.VISIBLE);
			} else if(item.getFlag()==false){
				holder.deletebt.setVisibility(View.GONE);
			}
		}
		
		final int pos=position;
		convertView.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub

				String mb_id=item.getMb_id();
				final String userid=mSessionManager.getUserid();
				
				if(userid.trim().equals(mb_id.trim()) == false){
					return ;
				}
				
				Button deletebt=(Button)v.findViewById(R.id.listitemcomment_delete);
				if(item.getFlag()==true){
					deletebt.setVisibility(View.GONE);
					item.setFlag(false);
				} else if(item.getFlag()==false){
					deletebt.setVisibility(View.VISIBLE);
					item.setFlag(true);
					
					deletebt.setOnClickListener(new OnClickListener(){
						public void onClick(View v) {
							// TODO Auto-generated method stub
							String comment_id=item.getWr_id().toString();
							
							Gson gson=new Gson();
							String board = item.getBoard();
							if(board.equals(ApiCode.NEWS) == false)
								board += "s"; 
							RequestData data = new RequestData(mContext,
									"/"+board+ApiCode.API_DELETECOMMENT, "POST", "");
							
							data.addParameter("userid", userid);
							data.addParameter("comment_id", comment_id);
							String sresult = data.getDataPost();
							BoardList boardList = gson.fromJson(sresult, BoardList.class);
							
							RequestGson requestGson=new RequestGson();
							if(boardList!=null){
								if(boardList.getResultCode().equals(ResultCode.SUCCESS)==true){
									requestGson.alertToast(mContext, "댓글 삭제 성공");
									items.remove(pos);
									notifyDataSetChanged();
								} else {
									requestGson.alertToast(mContext, "댓글 삭제 실패");
								}
							} else {
								requestGson.alertToast(mContext, "데이터 전송 실패");
							}
						}
					});
					
					
				}
			}
		});
		
		return convertView;
	}
}
